package Day11.test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * 斗地主发牌
 */
public class DouDiZhu {

    ArrayList<PuKe> ary = new ArrayList<>();

    /**
     * 初始化54张扑克牌
     */
    public void init(){
    ary.add(new PuKe("","大王",17));
    ary.add(new PuKe("","小王",16));

        String point = null;
        String type = null;
        int num = 0;

        for (int i = 0; i < 4; i++){
            if (i == 0){
                type = "♣";
            }else if (i == 1){
                type = "♠";
            }else if (i == 2){
                type = "♦";
            }else{
                type = "♥";
            }
            for (int j = 2; j < 15; j++){
                num = j;
                point = j + "";
                if (j == 11){
                    point = "J";
                }else if (j == 12){
                    point = "Q";
                }else if (j == 13){
                    point = "K";
                }else if (j == 14){
                    point = "A";
                }else if (j == 2){
                    num = 15;
                }
                ary.add(new PuKe(point,type,num));
            }
        }
    }

    /**
     * 洗牌
     */
    public void checkCard(){
        Collections.shuffle(ary);
    }

    /**
     * 发牌
     */
    public void sendCard(){
        ArrayList<PuKe> player1 = new ArrayList<>(ary.subList(0,17));
        ArrayList<PuKe> player2 = new ArrayList<>(ary.subList(17,34));
        ArrayList<PuKe> player3 = new ArrayList<>(ary.subList(34,51));

        myComparator(player1);
        myComparator(player2);
        myComparator(player3);

        System.out.println("玩家1的牌是");
        for (PuKe puck : player1 ){
            System.out.print(puck.getPoint() + " " + puck.getType());
        }

        System.out.println("\n玩家2的牌是");
        for (PuKe puck : player2 ){
            System.out.print(puck.getPoint() + " " + puck.getType());
        }

        System.out.println("\n玩家3的牌是");
        for (PuKe puck : player3 ){
            System.out.print(puck.getPoint() + " " + puck.getType());
        }
    }

    /**
     * 排序
     */
     public void myComparator(ArrayList ary){
         Collections.sort(ary, new Comparator<PuKe>() {
             @Override
             public int compare(PuKe o1, PuKe o2) {
                 return o1.getNum() - o2.getNum();
             }
         });
     }

    /**
     * 开始
     */
    public void process(){
        init();
        checkCard();
        sendCard();
    }
}
